﻿#include "logprocess.h"
#include "savelogthread.h"
#include "dataprocess.h"



//#pragma execution_character_set("utf-8")

LogProcess::LogProcess(QObject *parent) : QObject(parent)
{
    pLogFile = new QFile;
    pLogFileStream = new QDataStream;
    pCurrentCycle = new int(0);
}

LogProcess::~LogProcess()
{
    delete pLogFile;
    delete pLogFileStream;
    delete pCurrentCycle;
}


void LogProcess::readFile(const QString &file)
{
    if(false == isFileExist(file))
    {
        QMessageBox::warning(NULL, QString("错误"), QString("不存在文件:\"") + file + QString("\""));
        return;
    }

    pLogFile->setFileName(file);

    if(!pLogFile->open(QFile::ReadOnly))
    {
        QMessageBox::warning(NULL, QString("错误"), QString("文件打开失败:\"") + file + QString("\""));
        return;
    }

    pLogFileStream->setDevice(pLogFile);

    return;
}


unsigned int LogProcess::getTotalCycleNumber()
{
    QByteArray tmp;
    int count = 0;
    pLogFile->seek(0);
    while(!pLogFileStream->atEnd())
    {
        *pLogFileStream >> tmp;
        if(0 == count) {
            mStartDateTime = DataProcess::getDateTimeFromData(tmp, pFormatSetting);
        }
        count++;
    }

    mEndDateTime = DataProcess::getDateTimeFromData(tmp, pFormatSetting);

    pLogFile->seek(0);

    return count;
}



/* 根据周期步进值以及标志位获取数据
cycleNumber: 要读取的周期号
*/
QByteArray LogProcess::getCycleData(const int &cycleNumber)
{
    QByteArray bytes;

    int step =cycleNumber - *pCurrentCycle;

    int tmp = *pCurrentCycle;

    if(step <= 0)
    {
        *pCurrentCycle = 0;
        pLogFile->seek(0);
        if(tmp + cycleNumber - *pCurrentCycle > 0)
        {
            for(int i = 0; i < step + tmp; ++i)
            {
                *pLogFileStream >> bytes;
                ++(*pCurrentCycle);
            }
        }
    }
    else
    {
        for(int i = 0; i < step; ++i)
        {
            *pLogFileStream >> bytes;
            ++(*pCurrentCycle);
        }
    }

    return bytes;
}

void LogProcess::setFormatSetting(struct _format_settings *pFormatSetting)
{
    this->pFormatSetting = pFormatSetting;

    return;
}

QDateTime LogProcess::getStartDateTime()
{
    return mStartDateTime;
}

QDateTime LogProcess::getEndDateTime()
{
    return mEndDateTime;
}

void LogProcess::closeFile()
{
    pLogFile->close();
}




void LogProcess::logCaptureSlot(QDateTime start, QDateTime end, QString filePath)
{
    if (!this->pFormatSetting) return;
    SaveLogThread tSaveThread;


    pLogFile->seek(0);

    QVector<QByteArray*>* pCapturedLog = new QVector<QByteArray*>;
    pCapturedLog->clear();

    while(!pLogFileStream->atEnd())     // 在保存文件的线程函数里会清空该堆区内存, 忽略此处内存泄露的警告
    {
        QByteArray* byte = new QByteArray;
        QByteArray tmp;
        *pLogFileStream >> tmp;
        if (0 >= DataProcess::getDateTimeFromData(tmp, pFormatSetting).secsTo(start)
                && 0 <= DataProcess::getDateTimeFromData(tmp, pFormatSetting).secsTo(end)){
            *byte = QByteArray::fromHex(QByteArray::fromBase64(tmp));
            pCapturedLog->append(byte);
        }
    }

    tSaveThread.setSave(filePath, pCapturedLog);
    tSaveThread.run();
    tSaveThread.quit();
    tSaveThread.wait();

    return;
}
